package cn.push.embed.core;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.DrawableRes;

import cn.push.embed.core.utils.GetDeviceInfo;
import http.com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;

import cn.push.embed.config.BroadcastReceiverAction;
import cn.push.embed.config.Setting;
import cn.push.embed.core.utils.Logger;
import cn.push.embed.service.PushService;
import cn.push.embed.utils.CommonUtils;
import okhttp3.OkHttpClient;

import static android.content.Context.ALARM_SERVICE;

/**
 * Created by shangdongzhou on 2017/3/24.
 */

public class PushManager {
    private Context mContext;
    private static PushManager singleton = null;
    private boolean isFirst = true;
    private AlarmManager am;
    private PendingIntent pi;

    public synchronized static PushManager getInstance() {
        if (singleton == null) {
            singleton = new PushManager();
        }
        return singleton;
    }


    public static void setDebugMode(boolean mode) {
        Logger.setmDebug(mode);
    }

    public void init(Context mContext,String AppSecret,@DrawableRes int iconSourceId) {
        if (isFirst) {
            this.mContext = mContext;
            setAlarmClock();            //设置硬件时钟，为心跳提供事件驱动
            //启动推送服务的服务
            Intent service = new Intent(mContext, PushService.class);
            mContext.startService(service);
            //初始化直播空间
            initHtml();
            CommData.AppSecret=AppSecret;
            CommData.iconSourceId=iconSourceId;
            isFirst = false;
        }


    }

    //初始化http请求
    private void initHtml() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    /**
     * 设置硬件闹钟
     */
    private void setAlarmClock() {
       /* Intent service = new Intent(mContext, PushService.class);
        pi = PendingIntent.getService(mContext, 0, service, 0);
        am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Setting.intervalMillis, pi);*/
        Intent intent = new Intent(BroadcastReceiverAction.ACTION_ALARM_CLOCK);
        pi = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Setting.intervalMillis, pi);
    }




    /***
     * 关闭状态
     */
    public void close() {
        am.cancel(pi);
        am = null;
        isFirst = true;
    }

    /**
     * 设置用户昵称或者取消昵称，alisa 为空时取消，不为空时为注册
     *
     * @param alias
     */
    public void setAlias(String alias) {
        if (CommonUtils.isEmpty(alias)){
            alias= CommonUtils.EncryptMD5(GetDeviceInfo.getIdentity(mContext));
        }
        if (CommonUtils.isEmpty(alias)){
            PushService.handler.sendEmptyMessage(PushService.CANCLE_ALIAS);
            return;
        }
        Message message = new Message();
        message.what = PushService.REGISTER_ALIAS;
        Bundle bundle = new Bundle();
        bundle.putString("alias", alias);
        message.setData(bundle);
        PushService.handler.sendMessage(message);
    }

}
